import 'dart:convert';

import 'package:dio/dio.dart';
import 'package:flutter_yn_goods/config/app_config.dart';
import 'package:flutter_yn_goods/user/app_user_manager.dart';
import 'package:flutter_yn_goods/utils/log/utils_log.dart';
import 'package:get/get.dart';

class RequestInterceptor extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    // Map<String, Object> headers = {};
    var uuid = AppConfig.currentDeviceId;
    if (uuid.isNotEmpty) {
      options.headers['X-UUID'] = uuid;
    }
    var sessionId = AppUserManager().sessionId;
    if (sessionId.isNotEmpty) {
      options.headers['X-SESSIONID'] = sessionId;
    }
    var token = AppUserManager().getToken();
    if (token.isNotEmpty) {
      options.headers['Authori-zation'] = 'Bearer $token';
    }
    options.headers['cb-lang'] = "${Get.locale?.languageCode}-${Get.locale?.countryCode}" ;
     // options.headers['Authori-zation'] = 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJwd2QiOiJkNDFkOGNkOThmMDBiMjA0ZTk4MDA5OThlY2Y4NDI3ZSIsImlzcyI6Imd1by54aWFvbWluYnQuY29tIiwiYXVkIjoiZ3VvLnhpYW9taW5idC5jb20iLCJpYXQiOjE3NDY0NTM4NTUsIm5iZiI6MTc0NjQ1Mzg1NSwiZXhwIjoxNzQ5MDQ1ODU1LCJqdGkiOnsiaWQiOjExMDEwMiwidHlwZSI6ImxlYWRlciJ9fQ.HTng1-LTrBgN7voqHJi52YR8QdtWgy3bceOXpEwQO5Y';

    clog('daslkdjkas  ${options.headers}');
    handler.next(options);
    // logWrite("${options.uri.toString()}\n${options.data}");
  }

  @override
  Future onError(DioException err, ErrorInterceptorHandler handler) async {
    super.onError(err, handler);
    // logWrite("${err.requestOptions.uri.toString()}\n$err");
  }
}
